import java.awt.Color;
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * Team Huskies: Aakanksha Gaur, Alice Robinson, Paul Winters, and Steven Bradshaw
 * Course: TCSS 422
 * Assignment: Project 1
 * Date: April 26th 2011
 */

/**
 * This class is the user Interface for the Web Crawler
 * @author Team Huskies: Aakanksha Gaur, Alice Robinson, Paul Winters, and Steven Bradshaw
 * @version 1, April 26th 2011
 */
public class UserInterface
{
	/**
	 * Hard coded limit
	 */
	private static final int LIMIT = 10000;

	/**
	 * The data gatherer
	 */
	private DataGatherer my_gatherer;

	/**
	 * Time the program started
	 */
	private Long my_start_time;

	/**
	 * The number of threads to run.
	 */
	private static int the_thread_int;

	/**
	 * Used for GUI mode
	 */
	private static WebPanel my_display;

	/**
	 * Run as a gui or as console
	 */
	private static boolean my_gui_on;

	/**
	 * Main starter method
	 * 
	 * @param the_args the command line arguments
	 */
	public static void main(String ... the_args)
	{
		the_thread_int = 1;
		my_gui_on = false;
		my_display = new WebPanel();
		UserInterface UI = new UserInterface();
		UI.start();
	}

	/**
	 * The main component of the class
	 */
	public void start() {

		int n = JOptionPane.showOptionDialog(null, "What type of interface would you like to use?", 
				"Interface Select", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, 
				new Object[]{"GUI", "Console"}, "GUI");

		if(n == 0)
		{
			my_gui_on = true;
			starGUI();
		}
		else
		{
			my_gui_on = false;
			startConsole();
		}
	}

	/**
	 * Starts console mode.
	 */
	private void startConsole()
	{
		//		final Scanner input = new Scanner(System.in);
		//		final List<String> word_list = new ArrayList<String>();
		//		URL seed = null;
		//		int keywords = 0;
		//		int limit = 0;
		//		int threads = 0;
		//		
		//		System.out.println("Please enter a seed URL:");
		//		
		//		while(seed == null)
		//		{
		//			System.out.print("http://");
		//			
		//			try 
		//			{
		//				seed = new URL("http://" + input.next());
		//				System.out.println();
		//			} 
		//			catch(final MalformedURLException the_exception) 
		//			{
		//				System.out.println("URL is invalid, please enter another URL.");
		//				seed = null;
		//			}
		//		}
		//		
		//		System.out.println("You may search up to 10,000 webpages, what is the maximum number of pages you would like to search?");
		//		
		//		while(limit < 1 || limit > LIMIT)
		//		{
		//			System.out.print("Search Limit: ");
		//			limit = readInt(); //prevent Exception if user enters non-int.
		//			System.out.println();
		//		}
		//		
		//		System.out.println("How many threads would you like to use?");
		//		
		//		while(threads < 1)
		//		{
		//			System.out.print("Number of Threads: ");
		//			threads = readInt();
		//			System.out.println();
		//		}
		//			
		//		System.out.println("You may enter up to 10 keywords to search for, how many keywords would you like?");
		//		
		//		while(keywords < 1 || keywords > 10)
		//		{
		//			System.out.print("Number of Keywords: ");
		//			keywords = readInt();
		//			System.out.println();
		//		}
		//		
		//		System.out.println("Please enter " + keywords + " keywords.");
		//		while(word_list.size() < keywords)
		//		{
		//			System.out.print("Word #" + (word_list.size() + 1) + ": ");
		//			word_list.add(input.next());
		//		}
		//		
		//		input.close();

		my_start_time = System.currentTimeMillis();
		my_gatherer = new DataGatherer(new ArrayList<String>(Arrays.asList(new String[] {"intelligence", 
				"artificial", "agent", "university", "research", "science", "robot"})), 5000, this);
		my_gatherer.start("http://faculty.washington.edu/gmobus/", true, false, true);
	}

	public double getTime()
	{
		return (double) (System.currentTimeMillis() - my_start_time) / 1000;
	}

	public void printString(final String the_echo)
	{
		if(my_gui_on)
		{
			my_display.setBigString(the_echo, "");
		}
		else
		{
			System.out.println(the_echo);
		}
	}

	/**
	 * GUI mode.
	 */
	public void starGUI()
	{
		JFrame my_frame = new JFrame("Team Huskies Web Crawler");
		my_display.setPreferredSize(new Dimension(790, 590));
		my_display.setBackground(Color.WHITE);

		my_frame.add(my_display);	

		my_frame.pack();
		my_frame.setLocationRelativeTo(null);
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setResizable(false);
		my_frame.setVisible(true);


		String str = JOptionPane.showInputDialog(null, "How many pages should I crawl? Cancel choses default: ", "Max Pages", 1);

		int the_pages_int = LIMIT;

		try
		{
			the_pages_int = Integer.parseInt(str);
		}
		catch (NumberFormatException e)
		{
			the_pages_int = LIMIT;
		}

		String the_keyword_number = JOptionPane.showInputDialog(null, "Choose between 1 and 10 kewords. Cancel choses default: ", "Keywords", 1);

		int the_keyword_int = 10;

		try
		{
			the_keyword_int = Integer.parseInt(the_keyword_number);
		}
		catch (NumberFormatException e)
		{
			the_keyword_int = 10;
		}

		final List<String> the_word_list = new ArrayList<String>();

		for (int i = 1; i <= the_keyword_int; i++)
		{
			String the_insert = JOptionPane.showInputDialog(null, "Word " + i +" ? ", "Keywords", 1);

			if (the_insert == null)
			{
				the_word_list.add("Null");
			}
			else
			{
				the_word_list.add(the_insert);
			}
		}

		my_start_time = System.currentTimeMillis();
		my_gatherer = new DataGatherer(the_word_list, the_pages_int, this);

		URL seed = null;
		String the_url = JOptionPane.showInputDialog(null, "Which URL would you like to start at? Cancel gets you the default http://faculty.washington.edu/gmobus/ ", "URL Seed", 1);
		try 
		{
			seed = new URL(the_url);
		} 
		catch (MalformedURLException e) 
		{
			try 
			{
				seed = new URL("http://faculty.washington.edu/gmobus/");
			} 
			catch (MalformedURLException e1) 
			{
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		my_gatherer.start("http://faculty.washington.edu/gmobus/", true, false, true);

	}
}
